/*
 *   Copyright (C) 2009  Jens Kieselbach
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You may obtain a copy of the License at http://www.gnu.org/licenses/gpl.html
 */
package de.kieselbach.swing.table

import javax.swing.table.*

public class CommonTableModel extends AbstractTableModel {

    Closure valueGetter = {rowval, row, col ->
        rowval[col]
    }

    Closure valueSetter = {rowval, row, col, val ->
        rowval[col] = val
    }

    Closure valueEditable

    Closure valueClass

    // ------------------------------------------------------------------------

    List columns
    List rows

    CommonTableModel(List cols) {

        this(cols, null)
    }

    CommonTableModel(List cols, List r) {

        columns = cols
        rows = r != null ? r : [] as ArrayList
    }

    void clear() {

        int l = rows.size()-1
        rows.clear()
        if(l >= 0) {
            fireTableRowsDeleted(0, l)
        }
    }

    void addRow(def r) {

        rows << r
        fireTableRowsInserted(rows.size()-1, rows.size()-1)
    }

    void addRows(def insrows) {

        if(insrows != null) {
            int a = rows.size()-1

            for(def r : insrows) {
                rows << r
            }
            
            if(a != rows.size()-1) {
                fireTableRowsInserted(a, rows.size()-1)
            }
        }
    }

    void removeRows(def delrows) {

        def l = delrows as List
        if(l.size() > 0) {
            l.sort().reverse().each() {r ->
                rows.remove(r)
            }

            fireTableRowsDeleted(l.first() as int, l.last() as int)
        }
    }

    List getRows(def selrows) {

        def list = [] as List
        def sel = selrows as List
        if(sel.size() > 0) {
            sel.each() {r ->
                list << rows[r]
            }
        }

        list
    }

    void setRows(def r) {

        rows = r != null ? r : [] as ArrayList
        fireTableDataChanged()
    }

    // ------------------------------------------------------------------------

    public int getColumnCount() {

        columns != null ? columns.size() : 0
    }

    @Override
    public String getColumnName(int col) {

        columns[col] as String
    }

    public int getRowCount() {

        rows != null ? rows.size() : 0
    }

    public Object getValueAt(int row, int col) {

        valueGetter(rows[row], row, col)
    }

    @Override
    public boolean isCellEditable(int row, int col) {

        if(valueSetter != null) {
            return valueEditable != null ? valueEditable(rows[row], row, col) : true
        }
        else {
            return false
        }
    }

    @Override
    public void setValueAt(Object val, int row, int col) {

        valueSetter(rows[row], row, col, val)
        fireTableRowsUpdated(row, row) 
    }

    public Class<?> getColumnClass(int col) {

        valueClass != null ? valueClass(col) : Object.class
    }
}
